package com.cloudm.technician.ui.modules.personal.change_password

import android.os.Bundle
import android.view.View
import com.cloudm.technician.R
import com.cloudm.technician.extensions.string
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BaseFragment
import com.cloudm.technician.ui.widget.OnceClickListener
import kotlinx.android.synthetic.main.fragment_change_password.*

/**
 * 作者: wesley
 * 时间: 2017/12/26
 * 描述: ui
 */
class ChangePasswordFragment : BaseFragment<ChangePasswordPresenter>(),
        ChangePasswordContract.View {

    override val resource: Int
        get() = R.layout.fragment_change_password

    companion object {
        fun newInstance() = ChangePasswordFragment()
    }

    override fun initInject() {
        DaggerChangePasswordComponent.builder().appComponent(
                appComponent).changePasswordPresenterModule(
                ChangePasswordPresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {

    }

    override fun initVariables() {

    }

    override fun initViews(view: View) {

    }

    override fun getOldPassword(): String? = old_password.string()

    override fun getNewPassword(): String? = new_password.string()

    override fun getConfirmPassword(): String? = new_password_confirm.string()

    override fun showOlePasswordEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.please_input_old_password))
    }

    override fun showNewPasswordEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.please_input_new_password))
    }

    override fun showNewPasswordLengthMsg() {
        mContext.toast(mContext.resources.getString(R.string.password_length_lower_six))
    }

    override fun showNewConfirmNotNameMsg() {
        mContext.toast(mContext.resources.getString(R.string.password_not_same))
    }

    override fun showChangePasswordDialog() {
        showLoadingDialog(mContext.resources.getString(R.string.password_changing))
    }

    override fun hideChangePasswordDialog() {
        hideLoadingDialog()
    }

    override fun changePasswordFailure(errorMsg: String) {
        mContext.toast(errorMsg)
    }

    override fun changePasswordSuccess() {
        mContext.toast(mContext.resources.getString(R.string.password_change_success))
        val activity = mContext as? ChangePasswordActivity ?: return
        activity.finish()
    }

    override fun initListener() {
        submit.setOnClickListener(object : OnceClickListener() {
            override fun onOnceClick(view: View) {
                presenter?.changePassword(getOldPassword(), getNewPassword(), getConfirmPassword())
            }
        })
    }
}